<?php


class Patient extends CI_Model{

	public function new_patient(){
		if ($this->input->post('new_patient')=='1'){
			$data = array(
				'name'		=> $this->input->post('p_name') ,
				'password'	=> $this->input->post('p_password') ,
				'email'		=> $this->input->post('p_email') ,
				'age'		=> (int) $this->input->post('p_age') ,
				'weight'	=> (float) $this->input->post('p_weight') ,
				'height'	=> (float) $this->input->post('p_height')

			);
			$this->db->insert('patient', $data);
			return $this->db->insert_id();
		}
	}
	
	public function upload_patient(){
		if ($this->input->post('update_patient')=='1'){
			$id_patient		= $this->input->post('id_patient');
			$data = array(
				'name'		=> $this->input->post('p_name') ,
				'password'	=> $this->input->post('p_password') ,
				'email'		=> $this->input->post('p_email') ,
				'age'		=> (int) $this->input->post('p_age') ,
				'weight'	=> (float) $this->input->post('p_weight') ,
				'height'	=> (float) $this->input->post('p_height')
			);
			$this->db->where('id', $id_patient);
			$this->db->update('patient', $data); 

			return $this->db->insert_id();
		}
	}
	
	public function remove_patient(){
		if ($this->input->post('remove_patient')=='1'){
			$id_patient		= $this->input->post('id_patient');
			
			$this->db->where('id', $id_patient);
			$this->db->delete('patient'); 
		}
	}
	
	public function get_patient_all(){
		$query = $this->db->get('patient');
		return $query->result_array();
	}
	
	public function get_patient_by_id($id){
		$query = $this->db->get_where('patient', array('id' => $id));
		return $query->row_array();
	}
	
	function validate() {
        $this->db->where('email', $this->input->post('txt_email')); //@TODO sqlinjection
        $this->db->where('password', $this->input->post('txt_password')); //@TODO md5()

        $query = $this->db->get('patient'); 

        if ($query->num_rows == 1) { 
            return $query->row()->id; 
        }else{
			return 0;
		}
    }
	
	function login($id_patient) {
		$newdata = array(
			'id_p'	=> $id_patient,
			'email_p' => $this->input->post('txt_email'),
			'logged_p'		=> TRUE
		);

		$this->session->set_userdata($newdata);
	}
	
	function logout() {
		$array_items = array('id_p' => '', 'email_p' => '','logged_p' => '');
		$this->session->unset_userdata($array_items);
		
		redirect('/', 'refresh');
	}
	
	//Se estiver logado, passa direto ou vai para determinada pegina
	//Se nao vai para pagina de login
	function is_logged() {
        $logged = $this->session->userdata('logged_p');
		return ($logged);
    }
	
	//Se estiver logado, passa direto ou vai para determinada pegina
	//Se nao vai para pagina de login
	function logged($url='', $redirect_l=false, $redirect_nl=true) {
        $logged = $this->session->userdata('logged_p');
		if($logged == true){
			if($redirect_l) redirect('/'.$url, 'refresh');
		}
		else {
			if ($redirect_nl) redirect('/login', 'refresh');
		}
    }
	
	function changePswd() {
		$pswd1	= $this->input->post('txt_password1');
		$pswd2	= $this->input->post('txt_password2');
		
		if($pswd1==$pswd2){
			$id_patient = $this->session->userdata('id_patient');
			$data = array('password' => $pswd1);
			$this->db->where('id', $id_patient);
			$this->db->update('patient', $data); 
		}else{}//@TODO tratar caso digite senhas diferentes
		
		$this->logout();
	}
}


?>
